/*
 * MIT License
 *
 * Copyright (c) 2020 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.postman.root;

import cn.seaboot.commons.lang.P;
import cn.seaboot.postman.request.Request;
import cn.seaboot.postman.response.Response;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Objects;

/**
 * 文档节点
 * <p>
 * 节点有两个功能：
 * <p>
 * 1、表示一个完整的 API，每一个 {@link Item} 包含 {@link Request} 和 {@link Response}，
 * 2、用于组织树形结构，声明包含哪些子节点 {@link Item}
 * <p>
 * 默认排序规则：按照名称排序
 * <p>
 * Items are entities which contain an actual HTTP request, and sample responses attached to it.
 *
 * @author Mr.css
 * @version 2020-01-28 10:44
 */
public class Item implements Comparable<Item> {
    /**
     * 节点名称
     */
    private String name;
    /**
     * 子级节点
     */
    private Item[] item;
    /**
     * 请求
     */
    private Request request;
    /**
     * 响应
     */
    private Response[] response;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Item[] getItem() {
        return item;
    }

    public void setItem(Item[] item) {
        this.item = item;
    }

    public Request getRequest() {
        return request;
    }

    public void setRequest(Request request) {
        this.request = request;
    }

    public Response[] getResponse() {
        return response;
    }

    public void setResponse(Response... response) {
        this.response = response;
    }

    @Override
    public String toString() {
        return "Item{" +
                "name='" + name + '\'' +
                ", item=" + Arrays.toString(item) +
                ", request=" + request +
                ", response=" + Arrays.toString(response) +
                '}';
    }

    @Override
    public int compareTo(@NotNull Item o) {
        return P.collator().compare(this.name, o.name);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Item item = (Item) o;
        return Objects.equals(name, item.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}
